package com.yellowpage.tool;

import com.yellowpage.bigdata.service.AutoMailUserServiceImpl;
import com.yellowpage.mail.bean.MailBean;
import com.yellowpage.mail.service.MailSendService;
import com.yellowpage.mail.service.MailSendServiceImpl;
import com.yellowpage.nsp.listener.SynchroDataTimerTask;
import com.yellowpage.white.exception.MailSendException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.hssf.usermodel.*;

import java.io.*;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.Date;

/**
 * Created by Sun on 11/24/2015.
 */
public class Email {

    //作为一个邮件地址的内容类
    private  Map contentList = new HashMap();



    public String sendMail(int type) {


        Log log = LogFactory.getLog(this.getClass());

        //通过容器来实例化 maiSendService
        MailSendService mailSendService = (MailSendService)MyApplicationContextUtil.getBean("mailSendService");

        //windows 下的测试环境
        InputStream input = AutoMailUserServiceImpl.class.getClassLoader().getResourceAsStream("/constants.properties");
        Properties prop = new Properties();
        try {
            prop.load(input);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String doc = prop.getProperty("MAIL_FOLDER");

        try {

            //response.reset();
            InputStream in = SynchroDataTimerTask.class.getClassLoader().getResourceAsStream("/mail.properties");
            Properties props=new Properties();
            props.load(in);
            in.close();

            //获取用户姓名
            MailBean mail = new MailBean();
            mail.setUserName(props.getProperty("mail.userName"));
            mail.setPassWord(props.getProperty("mail.password"));

            //写邮件表头日期
            mail.setFrom(props.getProperty("mail.from"));

            //设计文件特定信息
            mail.setMessage(contentList.get("message").toString());
            mail.setSubject(contentList.get("subject").toString());

            //设置收件人
            List<String> userList = UserGet.getUserList(type,1);
            String to="";
            for (String string : userList) {
                to+=string+";";
            }

            to = "sunyinglong@shtel.com.cn";
            mail.setTo(to);


            //增加excel附件
            List<String> attachmentList = (List<String>)contentList.get("attachment");
            mail.setAttachmentList(attachmentList);

            //mail.setCc("szhou@guanghua.sh.cn");
            //mail.setBcc("szhou@guangua.sh.cn");
            try
            {
                mailSendService.sendMail(mail);
            }
            catch (MailSendException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (Exception e) {
            log.error(e.getMessage(), e);
            e.printStackTrace();
        }finally {

            //删除生成的附件
            if(contentList.get("attachment") != null) {
                List<String> attachmentList = (List<String>) contentList.get("attachment");

                for (Iterator i = attachmentList.iterator(); i.hasNext(); ) {
                    File file = new File(i.next().toString());
                    file.delete();
                }
            }
        }
        return  null;
    }

    //给出mail的格式, 当前为客户端统计的结果   附件格式
    public Map setMailContent1(String query_logDate,String query_logDate_end,String excelpath)
    {

        String mess = null;
        if(query_logDate_end.equals(" ")||query_logDate_end==null||query_logDate_end.length()==0){
            if(query_logDate.equals(" ")||query_logDate==null||query_logDate.length()==0){
                mess="日用户统计总览;	统计周期: 全部数据;    详情见附件";
            }else{
                mess="附件为:"+query_logDate+" 日 至 昨日   的客户端统计结果文件，请查收，谢谢";
            }

        }else{
            if(query_logDate.equals(" ")||query_logDate==null||query_logDate.length()==0){
                mess="日用户统计总览;	统计周期: 今天以前的全部数据;    详情见附件";
            }else{
                mess="附件为:"+query_logDate+" 日至"+query_logDate_end+" 日的客户端统计结果文件，请查收，谢谢";
            }

        }

        String date = new SimpleDateFormat("yyyy-MM-dd").format(new Date());

        //设置表头和信息
        contentList.put("message",mess);
        contentList.put("subject","客户端入编_" + date);

        //设置附件
        List<String> attachmentList = new ArrayList<String>();
        attachmentList.add(excelpath);
        contentList.put("attachment",attachmentList);

        return  contentList;

    }

    //给出mail的格式, 当前为客户端统计的结果   文本格式
    //统计昨天的日期
    public Map setMailContent2(String date,String content)
    {

        String mess = null;
        mess= date + "日数据如下:\n " + content + "请查收，谢谢";

        //设置表头和信息
        contentList.put("message",mess);
        contentList.put("subject","客户端入编_" + date);


        return  contentList;

    }




}
